#######################################################################
#Name of code file: sentiment_analysis.R

#Data In: sentiment_data.csv

#Data Out: Figures 3,4, and A2
######################################################################

#Load packages
library(dplyr)
library(ggplot2)
library(readr)

#Set Working Directory
setwd("../diresta_grossman_siegel_replication/")

#Read in Data
data<-read_csv("data/sentiment_data.csv")

#Plot Monthly Proportions by Sentiment and Source 

#Figure 3 (Refugees)
refugees<-subset(data, narrative=="Refugees (Pro)"|narrative=="Refugees (Anti)"|narrative=="Refugees (Neutral)")

refugees %>% 
  ggplot() + 
  aes(x = month, y = prop, color=narrative) + 
  geom_point(alpha = 0.5) + 
  geom_smooth(span=.7, alpha=.25, se=FALSE)+
  labs(y = "Monthly Prop. of Syria Posts", x = "Date")+
  theme_minimal(base_size=18)+
  facet_wrap(~ source)+
  theme(panel.spacing = unit(2, "lines"))
ggsave("plots/Figure3.pdf", width = 15, height = 8)


#Figure 4 (Clinton/Obama)
clinton_obama<-subset(data, narrative=="Clinton / Obama (Pro)"|narrative=="Clinton / Obama (Anti)"|narrative=="Clinton / Obama (Neutral)")

clinton_obama %>% 
  ggplot() + 
  aes(x = month, y = prop, color=narrative) + 
  geom_point(alpha = 0.5) + 
  geom_smooth(span=.7, alpha=.25, se=FALSE)+
  labs(y = "Monthly Prop. of Syria Posts", x = "Date")+
  theme_minimal(base_size=18)+
  facet_wrap(~ source)+
  theme(panel.spacing = unit(2, "lines"))
ggsave("plots/Figure4.pdf", width = 15, height = 8)


#Figure A2 (Trump)
trump<-subset(data, narrative=="Trump (Pro)"|narrative=="Trump (Anti)"|narrative=="Trump (Neutral)")

trump %>% 
  ggplot() + 
  aes(x = month, y = prop, color=narrative) + 
  geom_point(alpha = 0.5) + 
  geom_smooth(span=.7, alpha=.25, se=FALSE)+
  labs(y = "Monthly Prop. of Syria Posts", x = "Date")+
  theme_minimal(base_size=18)+
  facet_wrap(~ source)+
  theme(panel.spacing = unit(2, "lines"))
ggsave("plots/FigureA2.pdf", width = 15, height = 8)


